package com.ruoyi.framework.config.mp;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author wangjie
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("start insert fill ....");
        /*this.setFieldValByName("version", Constants.TRUE_INT,metaObject);
        this.setFieldValByName("tenant_id", Constants.TRUE_INT,metaObject);*/
        Object createBy = this.getFieldValByName("createBy", metaObject);
        if (metaObject.hasGetter("createBy") && createBy == null) {
            createBy = ShiroUtils.getLoginName();
            this.setFieldValByName("createBy", createBy, metaObject);
        }

        Object updateBy = this.getFieldValByName("updateBy", metaObject);
        if (metaObject.hasGetter("updateBy") && updateBy == null) {
            updateBy = ShiroUtils.getLoginName();
            this.setFieldValByName("updateBy", updateBy, metaObject);
        }

        Object createTime = this.getFieldValByName("createTime", metaObject);
        if (metaObject.hasGetter("createTime") && createTime == null) {
            Class<?> createTimeType = metaObject.getGetterType("createTime");
            if (createTimeType.equals(Date.class)) {
                this.strictInsertFill(metaObject,"createTime", Date::new,Date.class);
            } else if (createTimeType.equals(LocalDateTime.class)) {
                this.strictInsertFill(metaObject,"createTime", LocalDateTime::now,LocalDateTime.class);
            }
        }
        Object isDeleted = this.getFieldValByName("isDeleted", metaObject);
        if (metaObject.hasGetter("isDeleted") && isDeleted == null) {
            isDeleted = 0;
            this.setFieldValByName("isDeleted", isDeleted, metaObject);
        }
        doSetUpdateTime(metaObject);
//        this.strictInsertFill(metaObject, "createBy", String.class, ShiroUtils.getLoginName());
//        this.strictInsertFill(metaObject, "updateBy", String.class, ShiroUtils.getLoginName());
//        this.strictInsertFill(metaObject, "createTime", Date.class, DateUtils.getNowDate());
//        this.strictInsertFill(metaObject, "updateTime", Date.class, DateUtils.getNowDate());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("start update fill ....");
        Object updateBy = this.getFieldValByName("updateBy", metaObject);
        if (metaObject.hasGetter("updateBy") && updateBy == null) {
            updateBy = ShiroUtils.getLoginName();
            this.setFieldValByName("updateBy", updateBy, metaObject);
        }
        doSetUpdateTime(metaObject);
//        this.strictInsertFill(metaObject, "updateBy", Date.class, DateUtils.getNowDate());
//        this.strictUpdateFill(metaObject, "updateTime", Date.class, DateUtils.getNowDate());

    }

    private void doSetUpdateTime(MetaObject metaObject) {
        Object updateTime = this.getFieldValByName("updateTime", metaObject);
        if (metaObject.hasGetter("updateTime") && updateTime == null) {
            Class<?> updateTimeType = metaObject.getGetterType("createTime");
            if (updateTimeType.equals(Date.class)) {
                this.strictInsertFill(metaObject,"updateTime", Date::new,Date.class);
            } else if (updateTimeType.equals(LocalDateTime.class)){
                this.strictInsertFill(metaObject,"updateTime", LocalDateTime::now,LocalDateTime.class);
            }
        }
    }
}
